package com.ruoyi.system.Fdongbei.service.impl;

import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.StringUtils;

import com.ruoyi.system.Fdongbei.domain.TbGuozhong;
import com.ruoyi.system.Fdongbei.domain.TbHuayan;
import com.ruoyi.system.Fdongbei.domain.dto.ZidonghuaYanDto;
import com.ruoyi.system.Fdongbei.domain.vo.TbZiDongHuayanVo;
import com.ruoyi.system.Fdongbei.domain.vo.resvo.GyshzpjhyResVo;
import com.ruoyi.system.Fdongbei.domain.vo.resvo.HuayanYuan2ResVo;
import com.ruoyi.system.Fdongbei.domain.vo.resvo.HuayanYuanResVo;
import com.ruoyi.system.Fdongbei.mapper.TbHuayanMapper;
import com.ruoyi.system.Fdongbei.service.ITbHuayanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 化验--》Service业务层处理
 *
 * @author ruoyi
 * @date 2024-12-10
 */
@Service
@DataSource(value = DataSourceType.LY002_L)
public class TbHuayanServiceImpl implements ITbHuayanService {
    @Autowired
    private TbHuayanMapper tbHuayanMapper;

    /**
     * 查询化验--》
     *
     * @param id 化验--》主键
     * @return 化验--》
     */
    @Override
    public TbHuayan selectTbHuayanById(Long id) {
        return tbHuayanMapper.selectTbHuayanById(id);
    }

    @Override
    public TbGuozhong selectTbGuozhong(String chehao) {
        return tbHuayanMapper.selectTbGuozhongList(chehao);
    }

    /**
     * 查询化验--》列表
     *
     * @param tbHuayan 化验--》
     * @return 化验--》
     */
    @Override
    public List<TbHuayan> selectTbHuayanList(TbHuayan tbHuayan) {
        return tbHuayanMapper.selectTbHuayanList(tbHuayan);
    }


    /**
     * 供应商汇总平均化验
     *
     * @param gyshzpjhyResVo 化验--》
     * @return
     */
    @Override
    public List<GyshzpjhyResVo> selectTbHuayanhzList(GyshzpjhyResVo gyshzpjhyResVo) {


        return tbHuayanMapper.selectTbHuayanhzList(gyshzpjhyResVo);
    }

    /**
     * 原-化验日汇总
     *
     * @param huayanYuanResVo 化验--》
     * @return
     */
    @Override
    public List<HuayanYuanResVo> selectTbHuayanyuanList(HuayanYuanResVo huayanYuanResVo) {
        return tbHuayanMapper.selectTbHuayanyuanfaList(huayanYuanResVo);
    }


    /**
     * 原-化验日汇总2
     * @param huayanYuan2ResVo 化验--》
     * @return
     */
    @Override
    public List<HuayanYuan2ResVo> selectTbHuayanyuan2List(HuayanYuan2ResVo huayanYuan2ResVo) {
        return tbHuayanMapper.selectTbHuayanyuanfa2List(huayanYuan2ResVo);
    }

    /**
     * 新增化验--》
     *
     * @param tbHuayan 化验--》
     * @return 结果
     */
    @Transactional
    @Override
    public int insertTbHuayan(TbHuayan tbHuayan) {
        int rows = tbHuayanMapper.insertTbHuayan(tbHuayan);
        insertTbGuozhong(tbHuayan);
        return rows;
    }

    /**
     * 增加化验打印次数
     * @param czbh
     * @return
     */
    @Override
    public int upDateGuozhongDycs(String czbh) {
        int rows = tbHuayanMapper.updateGuozhongDycs(czbh);

        return rows;
    }

    /**
     * 修改化验--》
     *
     * @param tbHuayan 化验--》
     * @return 结果
     */
    @Transactional
    @Override
    public int updateTbHuayan(TbHuayan tbHuayan) {
        tbHuayanMapper.deleteTbGuozhongByCzbh(tbHuayan.getId());
        insertTbGuozhong(tbHuayan);
        return tbHuayanMapper.updateTbHuayan(tbHuayan);
    }

    /**
     * 批量删除化验--》
     *
     * @param ids 需要删除的化验--》主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTbHuayanByIds(Long[] ids) {
//        tbHuayanMapper.deleteTbGuozhongByCzbhs(ids);
        return tbHuayanMapper.deleteTbHuayanByIds(ids);
    }

    /**
     * 删除化验--》信息
     *
     * @param id 化验--》主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTbHuayanById(Long id) {
        tbHuayanMapper.deleteTbGuozhongByCzbh(id);
        return tbHuayanMapper.deleteTbHuayanById(id);
    }


    /**
     * 新增tb信息
     *
     * @param tbHuayan 化验--》对象
     */
    public void insertTbGuozhong(TbHuayan tbHuayan) {
        List<TbGuozhong> tbGuozhongList = tbHuayan.getTbGuozhongList();
        String id = tbHuayan.getCzbh();
        if (StringUtils.isNotNull(tbGuozhongList)) {
            List<TbGuozhong> list = new ArrayList<TbGuozhong>();
            for (TbGuozhong tbGuozhong : tbGuozhongList) {
                tbGuozhong.setCzbh(id);
                list.add(tbGuozhong);
            }
            if (list.size() > 0) {
                tbHuayanMapper.batchTbGuozhong(list);
            }
        }
    }


    public int updatehuayan(TbZiDongHuayanVo tbHuayan) {
        return tbHuayanMapper.updatehuayan(tbHuayan);
    }


}
